package com.tierstall.client.presenter;

import java.util.ArrayList;

import com.google.gwt.event.shared.HandlerManager;

import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.view.client.HasData;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.ProvidesKey;

import com.tierstall.client.service.WebServices;

import com.tierstall.client.view.content.CV_addGroup;
import com.tierstall.client.view.content.CV_showUsers;
import com.tierstall.client.view.content.CV_dbFilter;
import com.tierstall.client.view.main.ViewConstants;
import com.tierstall.shared.GruppeDTO;
import com.tierstall.shared.Logger;
import com.tierstall.shared.Page;
import com.tierstall.shared.UserDTO;
import com.tierstall.shared.UserType;

public class UserMGMTPresenter implements Presenter, CV_showUsers.Display {

	private final WebServices rpcService;
	private final HandlerManager eventBus;
	private final CV_showUsers view;
	private final MainPresenter mPresenter;
	// private final HomeView hView;
	private final String sessionId;
	private ArrayList<UserDTO> userList;
	private ListDataProvider<UserDTO> dataProvider;

	// private boolean isUpdating = false;

	public UserMGMTPresenter(WebServices rpcServices, String session_id,
			HandlerManager eventBus, MainPresenter mainPresenter,
			UserDTO currentUser) {
		this.rpcService = rpcServices;
		this.eventBus = eventBus;
		this.sessionId = session_id;
		this.mPresenter = mainPresenter;
		// this.hView = hView;
		view = (CV_showUsers) mPresenter.getContentView(Page.USER_LIST);
		// view = new CV_showUsers(new ViewConstants());
		this.dataProvider = view.getListDataProvider();
		this.view.setPresenter(this);
	}

	public void go(final HasWidgets container) {
//		createUser();
		getUsers();
		// hView.setContent(view);
		// container.clear();
		// container.add(view.asWidget());
	}

	/**
	 * The key provider that provides the unique ID of a contact.
	 */
	public final ProvidesKey<UserDTO> KEY_PROVIDER = new ProvidesKey<UserDTO>() {
		public Object getKey(UserDTO item) {
			return item == null ? null : item.getId();
		}
	};

	private void getUsers() {
		rpcService.getRpcUserService().getUserDetails(sessionId,
				new AsyncCallback<ArrayList<UserDTO>>() {

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						Logger.CLIENT_ERROR("UserMGMTPresenter: Failure retrieving user-list!");

					}

					@Override
					public void onSuccess(ArrayList<UserDTO> result) {
						userList = result;

						// userList = new ArrayList<UserDTO>();
						// userList.add(new UserDTO("4123", "username",
						// "password", UserType.ADMIN, "firstname", "lastname",
						// "email", "department"));
						// userList.add(new UserDTO("51234", "username2",
						// "password2", UserType.ADMIN, "firstname2",
						// "lastname2", "email2", "department2"));

						dataProvider.setList(userList);
						
						view.initCellTable(result);
						dataProvider.addDataDisplay(view.getDataProvider());
						dataProvider.refresh();
						// view.onInitialize();
						mPresenter.getMainView().setContent(view);

					}

				});

	}

	public void updateUser(final UserDTO user) {
	
		rpcService.getRpcUserService().updateUser(sessionId, user, new AsyncCallback<UserDTO>(){

			@Override
			public void onFailure(Throwable caught) {
				Logger.CLIENT_INFO("User : " + user.getUsername() + "update failed!!!");
				
			}

			@Override
			public void onSuccess(UserDTO result) {
				Logger.CLIENT_INFO("User : " + user.getUsername() + "updatet");
				
			}});
		
		
	}

	public void createUser(){
		rpcService.getRpcUserService().addUser(sessionId, new UserDTO(null, "bobby", "rosso", UserType.ADMIN, "Bobby", "Rosso", "bobby@rosso.de", "rosso inc."), new AsyncCallback<UserDTO>(){

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(UserDTO result) {
				Logger.CLIENT_INFO("User: " + result.getUsername() + " added!");
				
			}});
	}

	public ArrayList<UserDTO> getUserList() {
		return userList;
	}

	// public void setCellTable(CellTable<UserDTO> table) {
	// this.cellTable = table;
	// }

	@Override
	public void onAddClicked(String genehmigung, String leiter) {
		// TODO Auto-generated method stub

	}

	/**
	 * Add a display to the database. The current range of interest of the
	 * display will be populated with data.
	 * 
	 * @param display
	 *            a {@Link HasData}.
	 */
	public void addDataDisplay(HasData<UserDTO> display) {
		dataProvider.addDataDisplay(display);
	}

}
